﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using WoltersKluwer.SPChainGang.Core.ExtensionMethods;

namespace WoltersKluwer.SPChainGang.Core.Data.Entity
{
    public class ReportRecord : BaseEntity
    {
        public ReportRecord() : base(null)
        {
            Dummy = true;
        }

        public ReportRecord(SPListItem item) : base(item)
        {
        }

        public bool Dummy { get; protected set; }
        public new int Id
        {
            get { return Dummy ? default(int) : base.Id; }
        }

        public string OriginPage { get; set; }
        public string DetectedLink { get; set; }
        public string ReplacedWith { get; set; }

        string additionalInfo;
        public string AdditionalInfo
        {
            get { return additionalInfo; }
            set
            {
                if (!String.IsNullOrEmpty(additionalInfo))
                    additionalInfo += "; ";

                additionalInfo += value;
            }
        }

        public void LoadFrom(ReportRecord source)
        {
            OriginPage = source.OriginPage;
            DetectedLink = source.DetectedLink;
            ReplacedWith = source.ReplacedWith;
            AdditionalInfo = source.AdditionalInfo;
        }

        public override void Update()
        {
            if (Dummy)
                return;

            var originPage = SPEncode.UrlEncodeAsUrl(OriginPage);
            /* 
                         new SPFieldUrlValue
                              {
                                  Url = OriginPage,
                                  Description = OriginPage
                              };
             no URL type fields as they allow only 256 chars */

            Item.SetProperty(
                Constants.Fields.ReportListFields.Origin.InternalName, originPage);
            Item.SetProperty(Constants.Fields.ReportListFields.Detected.InternalName, DetectedLink);
            if (!ReplacedWith.IsNullOrEmpty())
                Item.SetProperty(Constants.Fields.ReportListFields.Replacement.InternalName, ReplacedWith);
            if (!AdditionalInfo.IsNullOrEmpty())
                Item.SetProperty(Constants.Fields.ReportListFields.AdditionalInfo.InternalName, AdditionalInfo);
            base.Update();
        }

        public override string Title
        {
            get
            {
                return Dummy ? null : base.Title;
            }
            set
            {
                if (!Dummy)
                    base.Title = value;
            }
        }

        protected new SPFieldLookupValue GetLookupValue(string lookupValue, string fieldInternalName)
        {
            return Dummy ? null : base.GetLookupValue(lookupValue, fieldInternalName);
        }

        public new void SystemUpdate()
        {
            if (!Dummy)
                base.SystemUpdate();
        }

    }
}
